
import 'dart:convert';

import 'package:flutter/material.dart';
import 'dart:developer' as developer;
import 'dart:convert' as convert;
import 'dart:io';
import 'package:http/http.dart' as http;
import 'package:dio/dio.dart';

///
///   基础的网络请求
///
///
class LearnApp05 extends StatelessWidget{
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return MaterialApp(
      title: "网络请求",
      theme: ThemeData.light(
      ),
      home:DioPage()
    );
  }

}



///  针对HttpClient 进行网络请求的方式
class HttpClientPage extends StatefulWidget{


  @override
  State createState() {
    return HttpClientPageSate();
  }
}

class HttpClientPageSate extends State<HttpClientPage>{
  String text="";
  @override
  Widget build(BuildContext context) {

    return Scaffold(
        appBar: AppBar(
          title: Text("HttpClient 请求"),
        ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          mainAxisSize: MainAxisSize.min,
          children: [
            Text(text.isEmpty ?"无数据":text),
            ElevatedButton(onPressed:(){
              getHttpClientData();
            }, child: Text("发送网络请求")),
            ElevatedButton(onPressed:(){
              setState(() {
                text = "";
              });;
            }, child: Text("清空数据"))
          ],
        ),
      ),
    );

  }




  getHttpClientData() async{
    developer.log("发送网络请求");
    try{
      HttpClient httpClient = HttpClient();
      HttpClientRequest  request  = await httpClient.getUrl(Uri.parse("http://wpt.net-east.com:8082/v1/"));

      HttpClientResponse  response = await  request.close();

      var result = await response.transform(convert.utf8.decoder).join();
      httpClient.close();
      setState(() {
        text = result;
      });
      developer.log("数据：$result");
    } catch(e){
      developer.log("异常原因：${e.toString()}");
    }finally{
      developer.log("finally");
    }
  }
}






///基于 Http 进行网络请求
///

class HttpPage extends StatefulWidget{

  @override
  State<HttpPage> createState() => HttpPageSate();
}

class HttpPageSate extends State<HttpPage>{
  String text="";
  @override
  Widget build(BuildContext context) {

    return Scaffold(
      appBar: AppBar(
        title: Text("HttpClient 请求"),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          mainAxisSize: MainAxisSize.min,
          children: [
            Text(text.isEmpty ?"无数据":text),
            ElevatedButton(onPressed:(){
              getHttpData();
            }, child: Text("发送网络请求")),
            ElevatedButton(onPressed:(){
              setState(() {
                text = "";
              });;
            }, child: Text("清空数据"))
          ],
        ),
      ),
    );

  }




  getHttpData() async {
    developer.log("发送网络请求");
    var url = Uri.http("wpt.net-east.com:8082","/v1/");

    var response =  await http.get(url);
    developer.log("状态： ${response.statusCode}");
    developer.log("正文： ${response.body}");
    if(response.statusCode==200){
     var  jsonResponse=  convert.jsonDecode(response.body) as Map<String,dynamic>;
     developer.log("数据：${jsonResponse}");
      setState(() {
        text = jsonResponse.toString();
      });
    }else{
      developer.log("网络请求失败！");

    }


  }
}






///基于 dio 进行网络请求
/// 多用于项目封装和框架

class DioPage extends StatefulWidget{

  @override
  State<DioPage> createState() => DioPageSate();
}

class DioPageSate extends State<DioPage>{
  String text="";
  @override
  Widget build(BuildContext context) {

    return Scaffold(
      appBar: AppBar(
        title: Text("HttpClient 请求"),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          mainAxisSize: MainAxisSize.min,
          children: [
            Text(text.isEmpty ?"无数据":text),
            ElevatedButton(onPressed:(){
              getDioData();
            }, child: Text("发送网络请求")),
            ElevatedButton(onPressed:(){
              setState(() {
                text = "";
              });;
            }, child: Text("清空数据"))
          ],
        ),
      ),
    );

  }




  getDioData() async {
    developer.log("发送网络请求");

    Response response;
    var dio = Dio();
    response = await dio.get("http://wpt.net-east.com:8082/v1/");

    developer.log("状态： ${response.statusCode}");
    developer.log("正文： ${response.data}");
    if(response.statusCode==200){
      setState(() {
        text = response.data.toString();
      });
    }else{
      developer.log("网络请求失败！");

    }


  }
}











